package site.itaoke.config;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.CellType;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by sunfeihu on 2017/11/4.
 */
public class TestController extends Controller {

    public void wlupload() {
        if (!"true".equals(this.getSessionAttr("login"))) {
            renderText("nopower");
            return;
        }
        render("/wlupload.ftl");
    }

    public void wlsearch() {
        render("/wlsearch.ftl");
    }

    public void searchWl() {
        String phone = this.getPara("phone");
        List<Record> list = Db.find(" select * from wuliu where phone = ? ", new Object[]{
                phone
        });
        String txt = "未找到对应的物流单号";
        if (null != list && list.size() > 0) {
            txt = "为您找到对应物流单号如下：<br />";
            for (Record r : list) {
                txt += "<input style='width:100%;border:1px solid #ccc;height:25px;' type='text' value='" + r.getStr("no") + "' /><br />";
            }
        }
        this.setAttr("txt", txt);
        render("/searchWl.ftl");
    }

    public void doWlUpload() throws IOException {
        if (!"true".equals(this.getSessionAttr("login"))) {
            renderText("nopower");
            return;
        }
        UploadFile uploadFile = this.getFile();
        File file = uploadFile.getFile();
        POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));
        HSSFWorkbook wb = new HSSFWorkbook(fs);
        HSSFSheet sheet = wb.getSheetAt(0);
        HSSFRow row = null;
        HSSFCell cell = null;
        List<Record> list = new ArrayList<Record>();
        Record record = null;
        for (int i = 1; i < sheet.getLastRowNum(); i++) {
            row = sheet.getRow(i);
            record = new Record();
            for (int j = 0; j < row.getLastCellNum(); j++) {
                cell = row.getCell(j);
                if (j == 0) {//收件人姓名
                    if (cell.getCellTypeEnum().equals(CellType.NUMERIC)) {
                        record.set("name", cell.getNumericCellValue());
                    } else {
                        record.set("name", cell.getStringCellValue());
                    }
                } else if (j == 1) {//收件人手机
                    if (cell.getCellTypeEnum().equals(CellType.NUMERIC)) {
                        record.set("phone", cell.getNumericCellValue());
                    } else {
                        record.set("phone", cell.getStringCellValue());
                    }
                } else if (j == 2) {//快递公司
                    if (cell.getCellTypeEnum().equals(CellType.NUMERIC)) {
                        record.set("company", cell.getNumericCellValue());
                    } else {
                        record.set("company", cell.getStringCellValue());
                    }
                } else if (j == 3) {//快递单号
                    if (cell.getCellTypeEnum().equals(CellType.NUMERIC)) {
                        record.set("no", cell.getNumericCellValue());
                    } else {
                        record.set("no", cell.getStringCellValue());
                    }
                }
                list.add(record);
            }
        }
        for (Record r : list) {
            if ("".equals(r.getStr("no"))) {
                continue;
            }
            Record czr = Db.findFirst(" select * from wuliu where no = ? ", new Object[]{
                    r.getStr("no")
            });
            //如果不存在，进行增加
            if (null == czr) {
                Db.save("wuliu", r);
            } else {//如果存在，进行修改
                r.set("id", czr
                        .getLong("id"));
                Db.update("wuliu", "id", r);
            }
        }
        wb.close();
        fs.close();
        FileUtils.deleteQuietly(file);
        renderText("ok");
    }

}
